package com.scy.java.thread;

public class ThreadStateChangeTest {
    public static void main(String[] args) {
        //t1打印1-100之间 的偶数
        Thread t1 = new Thread(){
            @Override
            public void run() {
                for (int i = 2; i <= 100;i +=2) {
                    System.out.println("偶数线程:"+i);
                    try {
                        //每隔一秒输出一次
                        //阻塞一秒
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        //启动t1线程
        t1.start();
        //t2打印1-100之间 的奇数数
        Thread t2 = new Thread(){
            @Override
            public void run() {
                for (int i = 1; i < 100; i += 2) {
                    System.out.println("奇数线程："+i);
                    if (i == 5){
//                        System.out.println("我即将暂停");
                        //打印到五，让奇数暂停两秒再继续
//                        Thread.yield();
//                        try {
//                            Thread.sleep(2000);
//                        } catch (InterruptedException e) {
//                            throw new RuntimeException(e);
//                        }
                        try {
                            t1.join();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
//                        System.out.println("我暂停了");
                    }
                }
            }
        };
        t2.start();
    }
}
